
import 'package:eshi/app/shi/data/HistoryMarks.dart';
import 'package:eshi/app/shi/data/System.dart';
import 'package:eshi/app/shi/data/parse/FileParser.dart';
import 'package:eshi/app/shi/widget/CommonWidgets.dart';
import 'package:eshi/main.dart';
import 'package:flutter/material.dart';

/// 基本文本标记 类 
abstract class BaseTextWidget extends StatefulWidget{
  final String text;
  final Widget child;
  final bool showChild;
  IconData? icon;
  late BuildContext context;
  late void Function(VoidCallback fn) setState;
  abstract HistoryMarks type;
  // //类得到的目录名
  // get cName {
  //   var cname = runtimeType.toString();
  //   return cname.substring(0,cname.length-10).toLowerCase();
  // }
  //文件的真实地址 
  String get assetPath => FileParser.getWholeName("assets/${type.name}/",text);
  String get fileName => assetPath.split("/").last.split(".").first;
  
  BaseTextWidget({super.key,required this.text, required this.child, this.showChild = true}){
    init();
  }
  
  init(){}
  
  @override
  TextfulWidgetState createState() => TextfulWidgetState(); 
  
  MarkBuild markBuild({required Function page,IconData? icon}){
     return MarkBuild(
      children:showChild ? child : null,
      page:page,
      dialog: onOpen ,
      icon:icon, 
      type:type.name,
      name:text
      );
  }

  onOpen() async{
    await simpleDialog(
      context: context, 
      body: page(),
      title : fileName 
    );
  }
  
  defaultOpen(){
    System.open(assetPath);
  }
  //重构这个方法来给出直接展示的样子 如果没有展示的页面就不用实现
  Widget page(){ return Text(""); }
  Widget iconPage({required Icon icon,Function? onClick }){
    return Container(
      padding: EdgeInsets.all(10),
      child: InkWell(
        onTap: () =>  onClick ==null ? onClick!(): defaultOpen(),
        customBorder: OutlineInputBorder(),
        child: Column(children: [
           icon,
           Text(fileName),
        ])
      )
    ); 
  }
  //重构这个方法 用来返回自定义的 渲染
  build(BuildContext context) {
    return  markBuild(page: page, icon:icon);
  }

  RemmberData remmber(Future<String> value){
      return RemmberData.create<String>(value,setState);
  }
  
}

class  RemmberData<T>  {
  T? value;
  final Future<T> _value;
  bool inited = false;
  static final Map<int,RemmberData> _datas={};
  static RemmberData create<T>(Future<T> value,void Function(VoidCallback fn) setState){
      if(_datas.containsKey(value.runtimeType.hashCode)) {
        return _datas[value.runtimeType.hashCode] as RemmberData;
      }

      return _datas[value.runtimeType.hashCode] = RemmberData<T>(value, setState);
  }
  RemmberData(this._value, void Function(VoidCallback fn) setState){

    switch(T){
      case String:
        value = "" as T;
      case bool:
        value = false as T;
      default :  
    }
    if(!inited) {
      _value.then((v) {
        value = v;
        setState((){} );   
        inited = true;
      });
    }
  }
}
/// 3种模式 
class MarkBuild{
  final Widget? children; //文本标准显示
  IconData? icon;
  final Function page;    //页面潜入内容
  final Function dialog;  //单击打开文件 默认是弹框显示 潜入内容 dialog=null的时候直接系统打开文件 
  String type;
  String name;
  Function? play;
  MarkBuild({required this.children,required this.page,required this.dialog,required this.type, required this.name, this.icon,this.play});

  Widget create(){
       return children!=null ?
         Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            GestureDetector(
                child: children,
                onTap: () {
                  dialog();
                },
                onLongPress: (){
                  customWidgetKey.currentState?.tabState?.onChangeName(type,name);
                },
            ),
            if(icon!=null)
              GestureDetector(
                onTap: play==null ? (){} :play!(),
                child: Icon(
                  icon,
                  color: Colors.blue,
                )
              )
          ],
        )
        : Column(
          mainAxisSize: MainAxisSize.min,
          children: [page()]
        );
  }
}
class TextfulWidgetState extends State<BaseTextWidget>{
  int refreshCount=0; 
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    widget.context = context;
    widget.setState = setState;

    final build = widget.build(context);
    switch(build.runtimeType){
      case MarkBuild:
        return build.create();
      case Widget:
        return build as Widget;
      default:
        return Center(child: Text( "not body!"));
    }
  }
}